package com.cheyun.netsalev3.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.utils.FunctionUtils
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import java.lang.Exception


/**
 * 默认头部
 */
class HeadView : RelativeLayout {
    var activity: Activity? = null

    init {

        activity = context as Activity
    }

    constructor(context: Context) : super(context) {}

    var left_show: Boolean = true
    var content_right: String = ""
    var content_title: String = ""

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.headView)
        left_show = a.getBoolean(R.styleable.headView_left_show, true)
        try {
            content_title = a.getString(R.styleable.headView_content_title)
        } catch (e: Exception) {

        }
        try {
            content_right = a.getString(R.styleable.headView_content_right)
        } catch (e: Exception) {

        }

        a.recycle()
        initView()
    }


    var rightBut: TextView? = null

    var titleText: TextView? = null
    var rightImg: ImageView? = null
    private fun initView() {
        var view = View.inflate(context, R.layout.head_layout, null)
        var layout = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
        view.layoutParams = layout
        var leftBut = view.findViewById<TextView>(R.id.leftBut)
        leftBut.visibility = if (left_show) View.VISIBLE else View.GONE
        leftBut.setOnClickListener {
            activity?.finish()
        }
        rightBut = view.findViewById<TextView>(R.id.rightBut)
        rightBut?.visibility = if (content_right != "") View.VISIBLE else View.GONE
        rightBut?.text = content_right
        titleText = view.findViewById<TextView>(R.id.titleText)
        titleText?.text = content_title
        rightImg = view.findViewById(R.id.rightImg)
        addView(view)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        this.layoutParams.height = FunctionUtils.dip2px(context, 32f)
    }


}